package com.homegoods.videomodule.ui.fragments;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cyt.chattlibrary.LiveKit;
import com.cyt.chattlibrary.inputboard.InputPanelListener;
import com.cyt.chattlibrary.message.GiftMessage;
import com.cyt.chattlibrary.message.TextMessage;
import com.cyt.chattlibrary.message.model.MessageContent;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.videomodule.R;
import com.homegoods.videomodule.databinding.VideoFragmentBottombarBinding;
import com.homegoods.videomodule.ui.animation.HeartLayout;
import com.homegoods.videomodule.ui.controller.ChatListAdapter;
import com.homegoods.videomodule.ui.view.LiveShowCommentSoftInputLayout;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.mvp.BasePresenter;

import java.util.Random;


public class BottomPanelFragment extends KBaseFragment<VideoFragmentBottombarBinding, BasePresenter> implements View.OnClickListener, Handler.Callback {

    private static final String TAG = "BottomPanelFragment";

    private ViewGroup buttonPanel;
    private ViewGroup inputPanel;
    private ViewGroup background;
    private ImageView btnGift;
    private ImageView btnHeart;
    private View btnShare;
    private View anchorHome;
    private HeartLayout heartLayout;
    private ChatListAdapter chatListAdapter;
    private LiveShowCommentSoftInputLayout liveShowCommentSoftInputLayout;
    private Handler handler = new Handler(this);
    private Random random = new Random();
    private String roomId;


    private OnShareListener onShareListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnShareListener) {
            onShareListener = (OnShareListener) activity;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LiveKit.removeEventHandler(handler);
    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {

        liveShowCommentSoftInputLayout = mBinding.llComment;

        buttonPanel = liveShowCommentSoftInputLayout.findViewById(R.id.button_panel);
        ImageView btnInput = liveShowCommentSoftInputLayout.findViewById(R.id.btn_input);
        inputPanel = liveShowCommentSoftInputLayout.findViewById(R.id.comment_bar);

        background = mBinding.background;
        ListView chatListView = liveShowCommentSoftInputLayout.findViewById(R.id.chat_listview);
        btnGift = liveShowCommentSoftInputLayout.findViewById(R.id.btn_gift);
        btnHeart = liveShowCommentSoftInputLayout.findViewById(R.id.btn_heart);
        heartLayout = liveShowCommentSoftInputLayout.findViewById(R.id.heart_layout);
        btnShare = liveShowCommentSoftInputLayout.findViewById(R.id.btn_share);
        anchorHome = liveShowCommentSoftInputLayout.findViewById(R.id.label1);

        chatListAdapter = new ChatListAdapter();
        chatListView.setAdapter(chatListAdapter);


        background.setOnClickListener(this);
        btnGift.setOnClickListener(this);
        btnHeart.setOnClickListener(this);
        btnShare.setOnClickListener(this);
        anchorHome.setOnClickListener(this);
        setInputPanelListener(text -> {
            final TextMessage content = TextMessage.obtain(text);
            LiveKit.sendMessage(content);
        });


        btnInput.setOnClickListener(v -> {
            buttonPanel.setVisibility(View.GONE);
            inputPanel.setVisibility(View.VISIBLE);
        });

        LiveKit.addEventHandler(handler);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.video_fragment_bottombar;
    }

    public void setInputPanelListener(InputPanelListener l) {
        liveShowCommentSoftInputLayout.setOnSendClickListener(l);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(background)) {
            onBackAction();
        } else if (v.equals(btnGift)) {
            GiftMessage msg = new GiftMessage("2", "送您一个礼物");
            LiveKit.sendMessage(msg);
        } else if (v.equals(btnHeart)) {
            heartLayout.post(() -> {
                int rgb = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
                heartLayout.addHeart(rgb);
            });
            GiftMessage msg = new GiftMessage("1", "为您点赞");
            LiveKit.sendMessage(msg);
        } else if (v.equals(btnShare)) {
            if (onShareListener != null) {
                onShareListener.onShare();
            }
        } else if (v.equals(anchorHome)) {
            ARouter.getInstance().
                    build(ARouterPath.PATH_ANCHOR_HOME)
//                    .withString("anchorId", id)
                    .navigation();

        }
    }

    /**
     * back键或者空白区域点击事件处理
     *
     * @return 已处理true, 否则false
     */
    public boolean onBackAction() {
        if (buttonPanel.getVisibility() != View.VISIBLE) {
            inputPanel.setVisibility(View.GONE);
            liveShowCommentSoftInputLayout.hideKeyBoardView();
            buttonPanel.setVisibility(View.VISIBLE);
            return true;
        }
        return false;
    }

    @Override
    public boolean handleMessage(android.os.Message msg) {
        switch (msg.what) {
            case LiveKit.MESSAGE_ARRIVED: {
                MessageContent content = (MessageContent) msg.obj;
                chatListAdapter.addMessage(content);
                break;
            }
            case LiveKit.MESSAGE_SENT: {
                MessageContent content = (MessageContent) msg.obj;
                chatListAdapter.addMessage(content);
                break;
            }
            case LiveKit.MESSAGE_SEND_ERROR: {
                break;
            }
            default:
        }
        chatListAdapter.notifyDataSetChanged();
        return false;
    }

    public interface OnShareListener {
        void onShare();
    }
}
